package com.booth.base.controllers;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.booth.base.model.GroupsModules;
import com.booth.base.model.ModuleActions;
import com.booth.base.model.Modules;
import com.booth.base.model.Users;
import com.booth.base.services.IModulesService;
import com.booth.base.services.impl.ModulesService;
import com.booth.base.utils.ResultStatus;
/***
 * 模块控制器
 * @TypeName：ModulesController
 * @description：
 * @author：Administrator
 */
public class ModulesController extends BaseController
{
  //菜单首页
  public void index()
  {
    render("modules.jsp");
  }
  //菜单添加页
  public void au_page()
  {
    Integer moduleId = getParaToInt("moduleId", Integer.valueOf(0));
    Modules modules = null;
    if ((moduleId != null) && (moduleId.intValue() > 0))
    {
      modules = (Modules)Modules.dao.findById(moduleId);
      if (modules != null)
      {
        Integer pid = modules.getInt("parent_id");
        if (pid != null)
        {
          Modules pm = (Modules)Modules.dao.findById(pid);
          setAttr("pid", pm.get("id"));
          setAttr("pname", pm.get("name"));
        }
      }
    }
    setAttr("modules", modules);
    render("modules_au.jsp");
  }
  /***
   * 获取上一级菜单，便于选择。
   */
  public void select_mod()
  {
    Integer type = getParaToInt("type", Integer.valueOf(0));
    Integer id = getParaToInt("id", Integer.valueOf(0));
    IModulesService service = new ModulesService();
    Short tp = 0;
    if(type != null && type > 0){
    	tp = Short.valueOf((short) (type.shortValue()-1));
    }
    List<Modules> lst = service.showParentModules(tp,id);
    renderJson(lst);
  }
  /***
   * 显示可用的菜单
   */
  public void show()
  {
    Integer type = getParaToInt("type", Integer.valueOf(0));
    IModulesService service = new ModulesService();
    List lst = service.showEnableModulesTree(Short.valueOf(type.shortValue()));
    renderJson(lst);
  }
  /***
   * 菜单列表页
   */
  public void list()
  {
    IModulesService service = new ModulesService();
    List lst = service.showModulesTree(null);
    setAttr("rows", lst);
    renderResult(ResultStatus.SUCCESS);
  }
  /***
   * 获取当前登录用户所具有的菜单。
   */
  public void showMenu()
  {
    HttpSession session = getSession();
    Users user = (Users)session.getAttribute("user");
    if (user != null)
    {
      IModulesService service = new ModulesService();
      List lst = service.showUserGroupMenu(user.getInt("group_id"));
      setAttr("rows", lst);
      renderResult(ResultStatus.SUCCESS);
    }
    else
    {
      renderResult(ResultStatus.NO_LOGIN);
    }
  }
  /***
   * 保存菜单
   */
  public void save()
  {
    Modules m = (Modules)getModel(Modules.class);
    if (m != null)
    {
    	if(m.get("type").equals("1")){
    		m.set("parent_id", null);
    	}else{
    		
    	}
    }
    boolean success = false;
    if ((m.getInt("id") == null) || (m.getInt("id").intValue() == 0))
    {
      success = m.save();
    }
    else
    {
      success = m.update();
    }
    renderResult(ResultStatus.SUCCESS);
  }
  /***
   * 获取要修改的菜单数据
   */
  public void update()
  {
    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
    if (id > 0)
    {
      Modules g = (Modules)Modules.dao.findById(Integer.valueOf(id));
      setAttr("module", g);
      renderResult(ResultStatus.SUCCESS);
    }
    else
    {
      renderResult(ResultStatus.UPDATE_FAILURE);
    }
  }
  /***
   * 删除菜单
   */
  public void delete()
  {
    boolean success = false;
    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
    if (id > 0)
    {
      GroupsModules.deleteByModulesId(id);
      ModuleActions.deleteByModuleId(id);
      success = new Modules().deleteById(Integer.valueOf(id));
    }
    renderResult(ResultStatus.SUCCESS);
  }
  /***
   * 显示可见菜单
   */
  public void showVisibleModule()
  {
    IModulesService service = new ModulesService();
    List lst = service.showUseModulesTree();
    renderJson(lst);
  }
}